package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by fuzhen.li on 2018/11/13.
 */
public class AvoidActivity extends BaseActivity implements CallBack<DateBean> {
    private FragmentSwitcher switcher;

    @BindView(R.id.spYear)
    MySpinner spYear;
    @BindView(R.id.spDanwei)
    MySpinner spDanwei;

    @BindView(R.id.iv_search)
    ImageView ivSearch;

    @BindView(R.id.tv_DW)
    TextView tvDanWei;

    @BindView(R.id.tv_QC)
    TextView tvQiCai;

    @BindView(R.id.tab_layout_other_business)
    TabLayout tabLayoutOtherBusiness;
    @BindView(R.id.vp_other_business)
    ViewPager vpOtherBusiness;

    private int indx = 0;
    private List<String> danwei = new ArrayList<>();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private String year = "";
    private int currentIndex = 0;
    private Fragment[] tabs;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // initFragmentSwitcher(savedInstanceState);
    }


    @Override
    public void initTitle() {

        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(AvoidActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(ConsTitles.EMERGENCY_AVOID_EXPLOD_1);
    }

    @Override
    public void initData() {
        super.initData();

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        spYear.getmText().setText(year + "");

        TabFragmentAdapter adapter = new TabFragmentAdapter(getSupportFragmentManager(), this);
        vpOtherBusiness.setAdapter(adapter);
        tabLayoutOtherBusiness.setupWithViewPager(vpOtherBusiness);
        switchTab(indx);
        spYear.setmDatas(TimeUtils.getYearList(), new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
                spYear.getmText().setText(TimeUtils.getYearList().get(position));
                ((EmergencyAvoidExplode1Activity) tabs[0]).requestData(spYear.getmText().getText().toString());
                ((EmergencyAvoidExplode2Activity) tabs[1]).requestData(spYear.getmText().getText().toString());
            }
        });
//        spYear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                DatePickerUtil.getInstance(AvoidActivity.this).setCallBack(AvoidActivity.this);
//            }
//        });
    }

    private void switchTab(int index) {
        if (vpOtherBusiness != null) {
            vpOtherBusiness.setCurrentItem(index, false);
        }
    }

    /*
     * 此结构为activity+fragment+fragment
     */
    class TabFragmentAdapter extends FragmentStatePagerAdapter {
        private String[] TITLES;



        private TabFragmentAdapter(FragmentManager fm, Activity context) {
            super(fm);
            TITLES = context.getResources().getStringArray(R.array.fangbaohuizong_activity_titles);
            tabs = new Fragment[TITLES.length];
            String year = spYear.getmText().getText().toString();
            tabs[0] = EmergencyAvoidExplode1Activity.newInstance(year);
            tabs[1] = EmergencyAvoidExplode2Activity.newInstance(year);
        }

        @Override
        public Fragment getItem(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_avoid;
    }

    @Override
    public void setListener() {
        vpOtherBusiness.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indx = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        danwei.add("单位");
//        danwei.add("防爆器材");
//        spYear.getmText().setText(sdf.format(new Date()));
//        spYear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                DatePickerUtil.getInstance(AvoidActivity.this).setCallBack(AvoidActivity.this);
//            }
//        });
//
//        spDanwei.setmDatas(danwei, new MySpinner.CallBack() {
//            @Override
//            public void onSelect(int position) {
//                //  currentIndex = position;
//            }
//        });
//        tvDanWei.setBackground(getResources().getDrawable(R.drawable.shape_rect_red));


    }

//    @OnClick({R.id.iv_search, R.id.tv_DW, R.id.tv_QC})
//    public void onTck(View v) {
//
//        switch (v.getId()) {
//
//            case R.id.tv_DW:
//                tvDanWei.setBackground(getResources().getDrawable(R.drawable.shape_rect_red));
//                tvQiCai.setBackground(getResources().getDrawable(R.drawable.shape_bg_stroke_r0));
//                setAvoid(0);
//                break;
//
//            case R.id.tv_QC:
//                tvDanWei.setBackground(getResources().getDrawable(R.drawable.shape_bg_stroke_r0));
//                tvQiCai.setBackground(getResources().getDrawable(R.drawable.shape_rect_red));
//                setAvoid(1);
//                break;
//            case R.id.iv_search:
//
//
//                break;
//        }
//
//    }

//    private void setAvoid(int currentIndex) {
//        year = spYear.getmText().getText().toString().substring(0, 4);
//        if (currentIndex == 0) {
//            switcher.switchToFragment(0);
//            EmergencyAvoidExplode1Activity emergencyAvoidExplode1Activity = (EmergencyAvoidExplode1Activity) switcher.getFragment(0);
//            emergencyAvoidExplode1Activity.setRequestData(baseUrl, year);
//        } else if (currentIndex == 1) {
//            switcher.switchToFragment(1);
//            EmergencyAvoidExplode2Activity emergencyAvoidExplode1Activity2 = (EmergencyAvoidExplode2Activity) switcher.getFragment(1);
//            emergencyAvoidExplode1Activity2.setRequestData(baseUrl, year);
//        }
//    }


//    private void initFragmentSwitcher(Bundle savedInstanceState) {
//        switcher = new FragmentSwitcher(getSupportFragmentManager(), R.id.rl_fragment_content);
//        switcher.addFragment(new EmergencyAvoidExplode1Activity(), "EmergencyAvoidExplode1Activity");
//        switcher.addFragment(new EmergencyAvoidExplode2Activity(), "EmergencyAvoidExplode2Activity");
//        //switcher.switchToFragment(0);
//        setAvoid(0);
//    }

    @Override
    public void onBack(DateBean dateBean) {
//        spYear.getmText().setText(dateBean.getmYear() + "");
//        ((EmergencyAvoidExplode1Activity) tabs[0]).requestData(spYear.getmText().getText().toString());
//        ((EmergencyAvoidExplode2Activity) tabs[1]).requestData(spYear.getmText().getText().toString());


        //year = dateBean.getmYear() + "";
//        if (currentIndex == 0) {
//            EmergencyAvoidExplode1Activity emergencyAvoidExplode1Activity = (EmergencyAvoidExplode1Activity) switcher.getFragment(0);
//            emergencyAvoidExplode1Activity.setRequestData(year);
//        } else if (currentIndex == 1) {
//            EmergencyAvoidExplode2Activity emergencyAvoidExplode1Activity2 = (EmergencyAvoidExplode2Activity) switcher.getFragment(1);
//            emergencyAvoidExplode1Activity2.setRequestData(year);
//        }

    }
}
